package com.changgou.seckill.timer;

import com.changgou.entity.DateUtil;
import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.service.SeckillGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @ClassName SeckillOrderPushRedis
 * @Description 定时任务
 * @Author 洛尘
 * @Date 16:29 2019/11/27
 * @Version 2.1
 **/
@Component
public class SeckillOrderPushRedis {

    @Autowired(required = false)
    private SeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 自定义定时任务
     */
    @Scheduled(cron = "0/15 * * * * ?  ")
    public void pushOrderRedis(){
        //获取当前的时间的5个时间段
        List<Date> dateMenus = DateUtil.getDateMenus();
        for (Date dateMenu : dateMenus) {
            String key_rule = DateUtil.data2str(dateMenu, DateUtil.PATTERN_YYYYMMDDHH);

            Example example = new Example(SeckillGoods.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("status",1);                                            //审核状态
            criteria.andGreaterThanOrEqualTo("startTime",dateMenu);                            //开始时间
            criteria.andLessThanOrEqualTo("endTime",DateUtil.addDateHour(dateMenu,2));  // 结束时间
            criteria.andGreaterThan("stockCount",0);                                    // 设置库存大于0
            // 判断去除重复的id
            Set ids = redisTemplate.boundHashOps("SeckillGoods_"+key_rule).keys();
            if(ids !=null && ids.size() > 0){
                criteria.andNotIn("id",ids);
            }

            List<SeckillGoods> seckillGoods = seckillGoodsMapper.selectByExample(example);
            System.out.println(seckillGoods.size());
            if( seckillGoods != null && seckillGoods.size() >0){
                for (SeckillGoods seckillGood : seckillGoods) {
                    // 将商品信息压入Redis中
                    redisTemplate.boundHashOps("SeckillGoods_"+key_rule).put(seckillGood.getId(),seckillGood);

                    // 获取当前的商品的id个数
                    Long[] idArrays = pushId(seckillGood.getStockCount(),seckillGood.getId());
                    // 把当前id 商品个数存入redisList
                    redisTemplate.boundListOps("SeckillGoodsCountList_"+seckillGood.getId()).leftPushAll(idArrays);
                    // 把当前的库存存入Redis中
                    redisTemplate.boundHashOps("SeckillGoodsCount").put(seckillGood.getId().toString(),seckillGood.getStockCount());
                }
            }
        }
    }

    /**
     * @author 洛尘
     * @Description 获取当前的id个数
     * @param stockCount 秒杀数
     * @param id 秒杀商品id
     * @date 2019/11/29 15:37
     * @return
    */
    private Long[] pushId(Integer stockCount, Long id) {
        Long[] ids = new Long[stockCount];
        for (int i = 0; i > stockCount; i++) {
            ids[i] = id;
        }
        return ids;
    }

}
